/* This file is part of the Crimsoon project
  * Copyright (C) 2009 Eduardo Henrique <eduardohleite@gmail.com>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
  * License as published by the Free Software Foundation; either
  * version 2 of the License, or (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Library General Public License for more details.
  *
  * You should have received a copy of the GNU Library General Public License
  * along with this library; see the file COPYING.LIB.  If not, write to
  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  * Boston, MA 02110-1301, USA.
*/

#include <QTreeWidget>
#include <QPixmap>
#include <QStringList>
#include <QSize>

#include <QStandardItemModel>
#include <QStandardItem>

#include <QtDebug>

#include "gallerytree.h"
#include "gallery.h"

void GalleryTree::setDefaultProperties()
{
    setHeaderHidden(true);
    setFixedWidth(240); // cannot be fixed

    QSize sizeForIcons(32,32);
    setIconSize(sizeForIcons);
}

void GalleryTree::refreshGallery()
{
    Gallery gallery;

    model = new QStandardItemModel(this);

    QStandardItem *parent = model->invisibleRootItem();
    QStandardItem *musicItem = new QStandardItem(QIcon(":img/music.png"), "Music");    

    parent->appendRow(new QStandardItem(QIcon(":img/nowplaying"), "Playing Now"));

    QStringList artists = gallery.getAllArtistNames();

    foreach (QString artist, artists)
    {
        QStandardItem *artistItem = new QStandardItem(QIcon(":img/artist.png"), artist);
        QStringList albums = gallery.getAllAlbunsByArtist(artist);

        foreach (QString album, albums)
        {
            Album currentAlbum = gallery.getAlbum(artist, album);

            QIcon icon;

            if (currentAlbum.picture != NULL && (!currentAlbum.picture->isEmpty()) && currentAlbum.picture->at(0)!= '\0')
            {
                QPixmap img;
                img.loadFromData(*currentAlbum.picture);
                icon = QIcon::QIcon(img);
            }
            else
            {
                icon = QIcon::QIcon(":img/album.png");
            }

            artistItem->appendRow(new QStandardItem(icon, album));
        }

        musicItem->appendRow(artistItem);
    }

    parent->appendRow(musicItem);
    this->setModel(model);
}

bool GalleryTree::isTopLevelItem(QModelIndex index)
{
    QStandardItem *item = model->itemFromIndex(index);
    return (item->parent() == NULL);
}

bool GalleryTree::isMusicIndexItem(QModelIndex index)
{
    QStandardItem *item = model->itemFromIndex(index);
    return (isTopLevelItem(index) && item->text() == "Music");
}

bool GalleryTree::isArtistItem(QModelIndex index)
{
    QStandardItem *item = model->itemFromIndex(index);
    return (item->parent() != NULL && item->hasChildren());
}

bool GalleryTree::isPlayingNowItem(QModelIndex index)
{
    QStandardItem *item = model->itemFromIndex(index);
    return (isTopLevelItem(index) && item->text() == "Playing Now");
}

bool GalleryTree::isAlbumItem(QModelIndex index)
{
    QStandardItem *item = model->itemFromIndex(index);
    return (item->parent() != NULL && item->parent()->parent() != NULL && (!item->hasChildren()));
}

GalleryTree::GalleryTree(QWidget *parent)
        :QTreeView(parent)
{
    setDefaultProperties();
    refreshGallery();
}

GalleryTree::~GalleryTree()
{
    delete model;
}
